package com.lovo.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.web.bean.ProductListingReviewBean;
import com.lovo.web.mapper.IProductListingReviewMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ProductListReviewServiceImpl {
    @Autowired
    IProductListingReviewMapper reviewMapper;
    @RabbitListener(queues = "queueAudit")
    public void acceptProductReview(String reviewMsg){
        //将得到的消息转化为bean
        ProductListingReviewBean review= JSONObject.parseObject(reviewMsg,ProductListingReviewBean.class);
        QueryWrapper<ProductListingReviewBean> qw = new QueryWrapper<>();
        qw.eq("pl_productName",review.getProductName()).orderByDesc("pk_reviewId").last("limit 1");
        ProductListingReviewBean bean = reviewMapper.selectOne(qw);
        if (bean != null){
            bean.setStatus(1);
            bean.setAdminName(review.getAdminName());
            bean.setOperateDate(review.getOperateDate());
            bean.setContent(review.getContent());
            reviewMapper.updateById(bean);
        }else {
            reviewMapper.insert(review);
        }
    }
}
